package com.yokit.c2c.controller;

import com.yokit.c2c.pojo.Shop;
import com.yokit.c2c.pojo.User;
import com.yokit.c2c.response.ResponseObject;
import com.yokit.c2c.service.ShopService;
import com.yokit.c2c.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author fengzeng
 * @create 2021/6/15 17:00
 */
@RestController
public class ShopController {
  @Resource
  private ShopService shopService;

  @Resource
  private UserService userService;

  /**
   * 申请成为商家
   * @param request
   * @return
   */
  @RequestMapping(value = "/createShop", method = RequestMethod.POST)
  public ResponseObject createShop(@RequestBody Map<String, String> request) {
    Shop shop = new Shop();

    //获取商铺名称
    String shopName = request.get("shopName");
    shop.setShopName(shopName);

    //获取商铺联系电话
    Integer shopMobile = Integer.parseInt(request.get("shopTel"));
    shop.setShopMobile(shopMobile);

    //设置商铺描述
    String shopDesc = request.get("shopDesc");
    shop.setShopDesc(shopDesc);

    //设置商铺用户
    Integer userId = Integer.parseInt(request.get("userId"));
    shop.setUserId(userId);

    //创建商铺
    shopService.createShop(shop);

    //更新用户状态
    User user = userService.selectByUserId(userId);
    //1表示用户是商家
    user.setIsShop("1");

    userService.updateUserInfo(user);

    return new ResponseObject(200, "商铺创建成功");

  }
}
